﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace Tree.Configuration
{
    public class ConfigurationEntry : ConfigurationElement
    {
        public ConfigurationEntry()
        {
        }

        [ConfigurationProperty("name")]
        public string Name
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("value")]
        public string Value
        {
            get
            {
                return (string)this["value"];
            }
            set
            {
                this["value"] = value;
            }
        }

        /// <summary>
        /// Gets this configuration value as a string. In case of failure, returns null.
        /// </summary>
        public string AsString
        {
            get
            {
                try
                {
                    return this["value"].ToString();
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Gets this configuration value as an int. In case of failure, returns int.MinValue.
        /// </summary>
        public int AsInteger
        {
            get
            {
                try
                {
                    return Convert.ToInt32(this["value"]);
                }
                catch
                {
                    return int.MinValue;
                }
            }
        }

        /// <summary>
        /// Gets this configuration value as a boolean. In case of failure, returns false.
        /// </summary>
        public bool AsBoolean
        {
            get
            {
                try
                {
                    return Convert.ToBoolean(this["value"]);
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// Gets this configuration value as a double. In case of failure, returns double.NaN.
        /// </summary>
        public double AsDouble
        {
            get
            {
                try
                {
                    return Convert.ToDouble(this["value"]);
                }
                catch
                {
                    return double.NaN;
                }
            }
        }
    }
}
